package cn.goktech.sports.modules.importDataone.controller;

import cn.goktech.sports.common.annotation.RestAnon;
import cn.goktech.sports.common.annotation.SysLog;
import cn.goktech.sports.common.entity.Page;
import cn.goktech.sports.common.entity.R;
import cn.goktech.sports.common.utils.ExcelUtils;
import cn.goktech.sports.common.utils.MD5Utils;
import cn.goktech.sports.modules.importDataone.entity.ImportDataoneEntity;
import cn.goktech.sports.modules.importDataone.entity.UserEntity;
import cn.goktech.sports.modules.importDataone.service.ImportDataoneService;
import cn.goktech.sports.modules.importDataone.service.UserService;
import cn.goktech.sports.modules.sys.controller.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 模板一数据
 *
 * @author ZhouChenglin<yczclcn @ 1 6 3 . com>
 */
@Api(value = "导入接口", description = "导入接口")
@RestController
@RequestMapping("/sys/importDataone")
public class ImportDataoneController extends AbstractController {

    @Autowired
    private ImportDataoneService importDataoneService;

    @Autowired
    private UserService userService;

    /**
     * 列表
     *
     * @param params
     * @return
     */
    @RequestMapping("/list")
    public Page<ImportDataoneEntity> list(@RequestBody Map<String, Object> params) {
        return importDataoneService.listImportDataone(params);
    }

    /**
     * 新增
     *
     * @param importDataone
     * @return
     */
    @SysLog("新增模板一数据")
    @RequestMapping("/save")
    public R save(@RequestBody ImportDataoneEntity importDataone) {
        return importDataoneService.saveImportDataone(importDataone);
    }

    /**
     * 根据id查询详情
     *
     * @param id
     * @return
     */
    @RequestMapping("/info")
    public R getById(@RequestBody Long id) {
        return importDataoneService.getImportDataoneById(id);
    }

    /**
     * 修改
     *
     * @param importDataone
     * @return
     */
    @SysLog("修改模板一数据")
    @RequestMapping("/update")
    public R update(@RequestBody ImportDataoneEntity importDataone) {
        return importDataoneService.updateImportDataone(importDataone);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @SysLog("删除模板一数据")
    @RequestMapping("/remove")
    public R batchRemove(@RequestBody Long[] id) {
        return importDataoneService.batchRemove(id);
    }


    @ResponseBody
    @RequestMapping(value = "getUploadData", method = {RequestMethod.POST})
    public String getRule(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws IOException {
        String name = request.getParameter("formData");
        ExcelUtils excelUtils = new ExcelUtils();
        List<String[]> strings = excelUtils.readExcel(file);
        for (String[] forstrings : strings) {
            System.out.println("==============" + forstrings);
            String machineNum = forstrings[0];

        }
        System.out.println("==========" + name);
        return "null";
    }

    /**
     * 功能描述: <br>
     * 增加/修改用户
     *
     * @param request 数据
     * @param file    excel文件
     * @return java.lang.String
     * @author PanY
     * @date 2019/10/12 14:45
     */
    @ResponseBody
    @ApiOperation("用户导入")
    @RestAnon
    @RequestMapping(value = "userExcelUp", method = {RequestMethod.POST})
    public R userExcelUp(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws IOException {
        //实例化工具类
        ExcelUtils excelUtils = new ExcelUtils();
        //获取excel数据
        List<String[]> strings = excelUtils.readExcel(file);
        int succ = 0;
        for (int i = 0; i < strings.size(); i++) {

            //第一次取出的是标题：【序号、账户、密码】
            if (i == 0) continue;

            //第二次取出的是需要的数据：【1、admin、张三、123456】
            String[] forstrings = strings.get(i);
            //后面还要加上MD5加密
            UserEntity user = new UserEntity();
            user.setLogin_name(forstrings[1]);
            user.setName(forstrings[2]);
            user.setPassword(MD5Utils.encrypt(forstrings[1], forstrings[3]));
            //调用操作数据增加用户（先根据数据进行判断是否有，如果有进行修改操作，否则执行新增）
            UserEntity userEntity = userService.selectUserByName(forstrings[2]);
            if (userEntity == null) {
                boolean b = userService.insertUserEntity(user);
                succ += b ? 1 : 0;
            }
            boolean b = userService.updateUserEntity(user);
            succ += b ? 1 : 0;
        }
        return R.ok("上传成功");
    }



    /**
     * 功能描述: <br>
     * 中速-DHL全球快递导入
     *
     * @param request
     * @param file
     * @return java.lang.String
     * @author PanY
     * @date 2019/10/14 17:47
     */
    @ApiOperation(value = "DHL数据导入")
    @ResponseBody
    @RequestMapping(value = "DHLGlobal", method = {RequestMethod.POST})
    public R MediumSpeed_DHLGlobalExpress(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws IOException {
        //实例化工具类
        ExcelUtils excelUtils = new ExcelUtils();
        //获取excel数据
        List<String[]> strings = excelUtils.readALLExcel(file);
        R r = importDataoneService.addDHL(strings);
        return r;
    }

    /**
     * 功能描述: <br>
     * 国际EMS数据导入
     *
     * @param request
     * @param file
     * @return java.lang.String
     * @author LuoYJ
     * @date 2019/10/14 17:47
     */
    @ApiOperation(value = "国际EMS数据导入")
    @ResponseBody
    @RequestMapping(value = "EMSGlobal", method = {RequestMethod.POST})
    public R MediumSpeed_EMSGlobalExpress(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws IOException {
        //实例化工具类
        ExcelUtils excelUtils = new ExcelUtils();
        //获取excel数据
        List<String[]> strings = excelUtils.readALLExcel(file);
        R r = importDataoneService.addEMS(strings);
        return r;
    }


    /**
     * 功能描述: <br>
     * E特快数据导入
     *
     * @param request
     * @param file
     * @return java.lang.String
     * @author LuoYJ
     * @date 2019/10/14 17:47
     */
    @ApiOperation(value = "E特快数据导入")
    @ResponseBody
    @RequestMapping(value = "EFastGlobal", method = {RequestMethod.POST})
    public R MediumSpeed_EFastestGlobalExpress(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws IOException {
        //实例化工具类
        ExcelUtils excelUtils = new ExcelUtils();
        //获取excel数据
        List<String[]> strings = excelUtils.readALLExcel(file);
        R r = importDataoneService.addEFastest(strings);
        return r;
    }

    /**
     * 功能描述: <br>
     * 航空运输数据导入
     *
     * @param request
     * @param file
     * @return java.lang.String
     * @author LuoYJ
     * @date 2019/10/14 17:47
     */
    @ApiOperation(value = "航空运输数据导入")
    @ResponseBody
    @RequestMapping(value = "AviationTransport", method = {RequestMethod.POST})
    public R MediumSpeed_AviationTransport(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws IOException {
        //实例化工具类
        ExcelUtils excelUtils = new ExcelUtils();
        //获取excel数据
        List<String[]> strings = excelUtils.readALLExcel(file);
        R r = importDataoneService.addAviationTransport(strings);
        return r;
    }

    /**
     * 功能描述: <br>
     * 验证是否是违禁品
     *
     * @param name 物品名称
     * @return java.lang.String
     * @author PanY
     * @date 2019/10/12 17:13
     */
    @ResponseBody
    @RequestMapping(value = "regGoodsName", method = {RequestMethod.POST})
    public String regGoodsName(@RequestParam("name") String name) {

        return null;
    }


    /**
     * 功能描述: <br>
     * 尺寸标准导入
     *
     * @param request
     * @param file
     * @return java.lang.String
     * @author LuoYongJun
     * @date 2019/10/22 16:51
     */
    @ApiOperation(value = "尺寸标准数据导入")
    @ResponseBody
    @RequestMapping(value = "addStandard", method = {RequestMethod.POST})
    public R addstandard(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws IOException {
        //实例化工具类
        ExcelUtils excelUtils = new ExcelUtils();
        //获取excel数据
        List<String[]> strings = excelUtils.readALLExcel(file);
        R r = importDataoneService.addStandard(strings);
        return r;
    }

    /**
     * 功能描述: <br>
     *  目的地国家导入
     *
     * @param request
     * @param file
     * @return java.lang.String
     * @author LuoYongJun
     * @date 2019/10/30
     */
    @ApiOperation(value = "目的地国家导入")
    @ResponseBody
    @RequestMapping(value = "addCountry", method = {RequestMethod.POST})
    public R addCountry(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws IOException {
        //实例化工具类
        ExcelUtils excelUtils = new ExcelUtils();
        //获取excel数据
        List<String[]> strings = excelUtils.readALLExcel(file);
        R r = importDataoneService.addCountry(strings);
        return r;
    }


    /**
     * 功能描述: <br>
     *  目的地国家导入
     *
     * @param request
     * @param file
     * @return java.lang.String
     * @author LuoYongJun
     * @date 2019/10/30
     */
    @ApiOperation(value = "目的地国家导入")
    @ResponseBody
    @RequestMapping(value = "addDepot", method = {RequestMethod.POST})
    public R addDepot(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws IOException {
        //实例化工具类
        ExcelUtils excelUtils = new ExcelUtils();
        //获取excel数据
        List<String[]> strings = excelUtils.readALLExcel(file);
        R r = importDataoneService.addDepot(strings);
        return r;
    }


    /**
     * 功能描述: <br>
     *  目的地国家导入
     *
     * @param request
     * @param file
     * @return java.lang.String
     * @author LuoYongJun
     * @date 2019/10/30
     */
    @ApiOperation(value = "目的地国家导入(带分区)")
    @ResponseBody
    @RequestMapping(value = "addLevelCountry", method = {RequestMethod.POST})
    public R addLevelCountry(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws IOException {
        //实例化工具类
        ExcelUtils excelUtils = new ExcelUtils();
        //获取excel数据
        List<String[]> strings = excelUtils.readALLExcel(file);
        R r = importDataoneService.addLevelCountry(strings);
        return r;
    }
    /**
     * 功能描述: <br>
     *  限重标注数据导入
     *
     * @param request
     * @param file
     * @return java.lang.String
     * @author LuoYongJun
     * @date 2019/10/31
     */
    @ApiOperation(value = "限重标准数据导入")
    @ResponseBody
    @RequestMapping(value = "addWeightLimit", method = {RequestMethod.POST})
    public R addWeightLimit(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws IOException {
        //实例化工具类
        ExcelUtils excelUtils = new ExcelUtils();
        //获取excel数据
        List<String[]> strings = excelUtils.readALLExcel(file);
        R r = importDataoneService.addWeightLimit(strings);
        return r;
    }

    /**
     * 功能描述: <br>
     *  国际EMS违禁品数据导入
     *
     * @param request
     * @param file
     * @return java.lang.String
     * @author LuoYongJun
     * @date 2019/11/11
     */
    @ApiOperation(value = "国际EMS违禁品数据导入")
    @ResponseBody
    @RequestMapping(value = "addBaseProhibited", method = {RequestMethod.POST})
    public R addBaseProhibited(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws IOException {
        //实例化工具类
        ExcelUtils excelUtils = new ExcelUtils();
        //获取excel数据
        List<String[]> strings = excelUtils.readExcel(file);
        R r = importDataoneService.addBaseProhibited(strings);
        return r;
    }

    /**
     * 功能描述: <br>
     *  头程违禁品数据导入
     *
     * @param request
     * @param file
     * @return java.lang.String
     * @author LuoYongJun
     * @date 2019/11/11
     */
    @ApiOperation(value = "头程违禁品数据导入")
    @ResponseBody
    @RequestMapping(value = "addHeaderProhibited", method = {RequestMethod.POST})
    public R addHeaderProhibited(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws IOException {
        //实例化工具类
        ExcelUtils excelUtils = new ExcelUtils();
        //获取excel数据
        List<String[]> strings = excelUtils.readALLExcel(file);
        R r = importDataoneService.addProhibitedArticle(strings);
        return r;
    }

    /**
     * 功能描述: <br>
     *  分区数据导入
     *
     * @param request
     * @param file
     * @return java.lang.String
     * @author LuoYongJun
     * @date 2019/11/18
     */
    @ApiOperation(value = "分区数据导入")
    @ResponseBody
    @RequestMapping(value = "addCountryPartition", method = {RequestMethod.POST})
    public R addCountryPartition(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws IOException {
        //实例化工具类
        ExcelUtils excelUtils = new ExcelUtils();
        //获取excel数据
        List<String[]> strings = excelUtils.readALLExcel(file);
        R r = importDataoneService.addCountryPartition(strings);
        return r;
    }
    /**
     * 功能描述: <br>
     *  分区数据导入
     *
     * @param request
     * @param file
     * @return java.lang.String
     * @author LuoYongJun
     * @date 2019/11/18
     */
    @ApiOperation(value = "产品分区数据导入")
    @ResponseBody
    @RequestMapping(value = "addProductPartition", method = {RequestMethod.POST})
    public R addProductPartition(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws IOException {
        //实例化工具类
        ExcelUtils excelUtils = new ExcelUtils();
        //获取excel数据
        List<String[]> strings = excelUtils.readALLExcel(file);
        R r = importDataoneService.addProductPartition(strings);
        return r;
    }

    /**
     * 功能描述: <br>
     *  分区数据导入
     *
     * @param request
     * @param file
     * @return java.lang.String
     * @author LuoYongJun
     * @date 2019/11/18
     */
    @ApiOperation(value = "中速FedEx数据导入")
    @ResponseBody
    @RequestMapping(value = "addFexEx", method = {RequestMethod.POST})
    public R addFexEx(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws IOException {
        //实例化工具类
        ExcelUtils excelUtils = new ExcelUtils();
        //获取excel数据
        List<String[]> strings = excelUtils.readALLExcel(file);
        R r = importDataoneService.addFed(strings);
        return r;
    }

    /**
     * 功能描述: <br>
     *  导入直达产品区间计费表数据
     *
     * @param request
     * @param file
     * @return java.lang.String
     * @author LuoYongJun
     * @date 2019/11/18
     */
    @ApiOperation(value = "导入直达产品区间计费表数据")
    @ResponseBody
    @RequestMapping(value = "addThroughSectionCharging", method = {RequestMethod.POST})
    public R addThroughSectionCharging(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws IOException {
        //实例化工具类
        ExcelUtils excelUtils = new ExcelUtils();
        //获取excel数据
        List<String[]> strings = excelUtils.readALLExcel(file);
        R r = importDataoneService.addThroughSectionCharging(strings);
        return r;
    }


    /**
     * 功能描述: <br>
     *  导入dhl前30计费区间数据
     *
     * @param request
     * @param file
     * @return java.lang.String
     * @author LuoYongJun
     * @date 2019/11/22
     */
    @ApiOperation(value = "导入dhl前30计费区间数据")
    @ResponseBody
    @RequestMapping(value = "addDHLChargingSection", method = {RequestMethod.POST})
    public R addDHLChargingSection(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws IOException {
        //实例化工具类
        ExcelUtils excelUtils = new ExcelUtils();
        //获取excel数据
        List<String[]> strings = excelUtils.readALLExcel(file);
        R r = importDataoneService.addDHLChargingSection(strings);
        return r;
    }
}
